package com.acwing;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 滑动窗口最大值
 */
public class No79 {
    public int[] maxInWindows(int[] nums, int k) {
        Deque<Integer> deque = new ArrayDeque<>();
        int[] res = new int[nums.length - k + 1];
        for (int i = 0; i < nums.length; i++) {
            int left = i - k;
            if (left >= 0 && !deque.isEmpty() && deque.peekFirst() == nums[left]) {
                deque.pollFirst();
            }
            while (!deque.isEmpty() && deque.peekLast() < nums[i]) {
                deque.pollLast();
            }
            deque.offerLast(nums[i]);
            if (left + 1 >= 0 && !deque.isEmpty()) {
                res[left + 1] = deque.peekFirst();
            }
        }
        return res;
    }
}
